package com.qxzn.app.page.robot;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.qxzn.app.bean.RobotDeviceListBean;
import com.qxzn.app.page.R;

import java.util.List;

public class RobotSpinnerAdapter extends RecyclerView.Adapter<RobotSpinnerAdapter.ViewHolder> {
    List<RobotDeviceListBean.DataBean> dataList;

    public RobotSpinnerAdapter(List<RobotDeviceListBean.DataBean> robotDataList) {
        dataList = robotDataList;
    }

    @NonNull
    @Override
    public RobotSpinnerAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.spinner_item_layout, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        RobotDeviceListBean.DataBean dataBean = dataList.get(position);
        holder.itemView.setTag(dataBean);
        boolean current = "1".equals(dataBean.getCurrent());
        int drawable = 0;
        if (1 == dataBean.getOntype()) {
            holder.itemView.setSelected(true);
            holder.state.setText("在线");
            if (current)
                drawable = R.drawable.ic_check_24dp;
        } else {
            holder.itemView.setSelected(false);
            holder.state.setText("离线");
            if (current)
                drawable = R.drawable.ic_check_gray_24dp;
        }
        holder.state.setCompoundDrawablesWithIntrinsicBounds(null, null, drawable == 0 ? null : holder.itemView.getResources().getDrawable(drawable), null);
        holder.name.setText(dataBean.getName());
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView name;
        TextView state;

        public ViewHolder(View itemView) {
            super(itemView);
            name = itemView.findViewById(R.id.spinner_item_name);
            state = itemView.findViewById(R.id.spinner_item_state);
        }
    }
}
